package com.ht.modules.ai.app.controller;

import java.util.List;
import java.util.Map;
import java.io.IOException;

import jakarta.servlet.http.HttpServletRequest;
import com.ht.aspect.annotation.AutoLog;
import com.ht.entity.LoginUser;
import com.ht.entity.Result;
import com.ht.modules.ai.app.entity.AppInfo;
import com.ht.modules.ai.app.entity.AppInfoVo;
import com.ht.modules.ai.app.service.IAppInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 应用信息
 */
@Slf4j
@Api(tags = "应用信息")
@RestController
@RequestMapping("/app/appInfo")
public class AppInfoController {

    @Autowired
    private IAppInfoService appInfoService;

    /**
     * 分页列表查询
     *
     * @param appInfoVo 参数
     * @param pageNo    页码
     * @param pageSize  当页大小
     * @param column    默认排序列
     * @param order     默认排序方式
     * @return 分页列表
     */
    @ApiOperation(value = "应用信息-分页列表查询", notes = "应用信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AppInfoVo appInfoVo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(name = "column", defaultValue = "appName") String column,
                                   @RequestParam(name = "order", defaultValue = "desc") String order) {
        Page<AppInfoVo> page = new Page<>(pageNo, pageSize);
        page = appInfoService.list(page, appInfoVo, column, order);
        return Result.ok(page);
    }

    /**
     * 不分页列表查询
     *
     * @param appInfoVo 参数
     * @param column    默认排序列
     * @param order     默认排序方式
     * @return 不分页列表
     */
    @ApiOperation(value = "应用信息-不分页列表查询", notes = "应用信息-不分页列表查询")
    @GetMapping(value = "/listNoPage")
    public Result<?> queryPageList(AppInfoVo appInfoVo,
                                   @RequestParam(name = "column", defaultValue = "appName") String column,
                                   @RequestParam(name = "order", defaultValue = "desc") String order) {
        List<AppInfoVo> list = appInfoService.listNoPage(appInfoVo, column, order);
        return Result.ok(list);
    }

    /**
     * 添加
     *
     * @param appInfo 参数
     * @return 操作结果
     */
    @AutoLog(value = "应用信息-添加")
    @ApiOperation(value = "应用信息-添加", notes = "应用信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AppInfo appInfo) {
        appInfoService.add(appInfo);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param appInfo 参数
     * @return 操作结果
     */
    @AutoLog(value = "应用信息-编辑")
    @ApiOperation(value = "应用信息-编辑", notes = "应用信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AppInfo appInfo) {
        appInfoService.edit(appInfo);
        return Result.ok("修改成功！");
    }

    /**
     * 通过id删除（逻辑删除）
     *
     * @param id 参数
     * @return 操作结果
     */
    @AutoLog(value = "应用信息-通过id删除")
    @ApiOperation(value = "应用信息-通过id删除", notes = "应用信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") Integer id) {
        appInfoService.delete(id);
        return Result.ok("删除成功！");
    }

    /**
     * 批量删除（逻辑删除）
     *
     * @param ids 参数
     * @return 操作结果
     */
    @AutoLog(value = "应用信息-批量删除")
    @ApiOperation(value = "应用信息-批量删除", notes = "应用信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        appInfoService.deleteBatch(ids);
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id 参数
     * @return 查询结果
     */
    @ApiOperation(value = "应用信息-通过id查询", notes = "应用信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") Integer id) {
        AppInfo entity = appInfoService.queryById(id);
        return Result.ok(entity);
    }

    /**
     * 通过id查询Vo
     *
     * @param id 参数
     * @return 查询结果
     */
    @ApiOperation(value = "应用信息-通过id查询Vo", notes = "应用信息-通过id查询Vo")
    @GetMapping(value = "/queryVoById")
    public Result<?> queryVoById(@RequestParam(name = "id") Integer id) {
        AppInfoVo entityVo = appInfoService.queryVoById(id);
        return Result.ok(entityVo);
    }

    /**
     * 导出excel
     *
     * @param appInfoVo 参数
     * @param column    默认排序列
     * @param order     默认排序方式
     * @return 导出结果
     */
    @GetMapping(value = "/exportXls")
    public ModelAndView exportXls(AppInfoVo appInfoVo,
                                  @RequestParam(name = "column", defaultValue = "appName") String column,
                                  @RequestParam(name = "order", defaultValue = "desc") String order) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<AppInfoVo> list = appInfoService.listNoPage(appInfoVo, column, order);
        //导出文件名称
        String title = "应用信息";
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, AppInfoVo.class);
        ExportParams exportParams = new ExportParams(title + "列表数据", "导出人:" + sysUser.getRealname(), "导出信息");
        exportParams.setAddIndex(true);
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request 请求
     * @return 操作结果
     */
    @PostMapping(value = "/importExcel")
    public Result<?> importExcel(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<AppInfo> importList = ExcelImportUtil.importExcel(file.getInputStream(), AppInfo.class, params);
                appInfoService.importExcel(importList);
                return Result.ok("文件导入成功！数据行数:" + importList.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return Result.error("文件导入失败！");
    }

}
